<?php
/**
 * @version		$Id: almwhm.php 8 2011-12-23 15:25:34Z netix.mail@gmail.com $
 * @package		ALM Hoster
 * @desription	Web hosting and domain menager for resellers
 * @subpackage	Components
 * @copyright	Copyright (C) 2011 ALM Studio. All rights reserved.
 * @author		ALM Studio
 * @email		info@alm-studio.net
 * @link		http://alm-studio.net
 * @license		Commercial
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
$document	= & JFactory::getDocument();

	function almhosterCpanelIkone( $link, $image, $text, $javascript='', $class='' ) 
	{
		$image = '../media/com_almhoster/images/' . $image;
	?>
		<div style="float:left;">
			<div class="icon">
				<a <?php echo $class; ?> href="<?php echo $link; ?>" <?php echo $javascript; ?> >
					<img src="<?php echo $image; ?>" alt="<?php echo $text; ?>" align="top" border="0" />
					<span><?php echo $text; ?></span>
				</a>
			</div>
		</div>
	<?php 
	}

	function almhosterCpanelPopup($class, $link, $image, $text, $javascript='' ) 
	{
		$image = '../media/com_almhoster/images/' . $image;
	?>
		<div style="float:left;">
			<div class="icon">
				<a class="<?php echo $class; ?>" rel="{handler: 'iframe', size: {x: 875, y: 550}, onClose: function() {}}" href="<?php echo $link; ?>" <?php echo $javascript; ?> >
					<img src="<?php echo $image; ?>" alt="<?php echo $text; ?>" align="top" border="0" />
					<span><?php echo $text; ?></span>
				</a>
			</div>
		</div>
	<?php 
	}
	
	function mail_data($tmplkey) {
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('*');
		$query->from('#__almh_email_tmpl');
		$query->where("TmplKey = '$tmplkey'");
		$db->setQuery($query);
		$db->setQuery((string) $query);	
			if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
		$mail_data = $db->loadObject();
	
		return $mail_data;	
	}
	
	function send_email($tmpl,$recipient) {
	
		$app	= JFactory::getApplication();
		jimport( 'joomla.mail.helper' );
		$config = JFactory::getConfig();
		$params = JComponentHelper::getParams('com_almhoster');	
		$maildata = mail_data($tmpl);
		$cli = client_data($recipient);
		
		$subject_text = $maildata->TmplSubjekt;
		$from_name = $maildata->TmplFromName;
		$from_email = $maildata->TmplFromEmail;

		if ($tmpl == 'invoice_created'){
			$body = $maildata->TmplText;
			$invoice = invoice_data($cli->ClientId);
			$body = str_replace('{ClientName}', $cli->ClientName, $email_content);
			$body = str_replace('{InvoiceNumber}', $invoice->InvoiceId, $email_content);
			$body = str_replace('{InvoiceForOrder}', $invoice->InvoiceOrder, $email_content);
			$body = str_replace('{InvoiceFromDate}', $invoice->InvoiceDate, $email_content);
			$body = str_replace('{InvoiceDueDate}', $invoice->InvoiceDue, $body);
			$body = str_replace('{InvoiceForProduct}', $invoice->InvoiceProduct, $body);
			$body = str_replace('{AmountToPay}', $invoice->InvoiceAmount, $email_content);
		}
		$subject = JMailHelper::cleanSubject($subject_text);		
		$body    = JMailHelper::cleanBody($body);
			
		$email = JFactory::getMailer();
		$email->setSender(array($from_email,$from_name));
		$email->addRecipient($cli->email);
		$email->setSubject($subject_text);
		$email->CharSet = "utf-8";
		$email->setBody($body);
		$email->isHTML();
		if (!$email->Send()) {
					JError::raiseWarning(500, JText::_('Greska pri slanju obavjestenja na email uposlenika, moguce da u detalje nije unesen email.'));
				}	
		return true;		
	}

	function server_data($server){
	
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('*');
		$query->from('#__almh_servers');
		$query->where('ServerId = '.$server);
		$db->setQuery($query);
		$db->setQuery((string) $query);
		
			if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
		$server_data = $db->loadObject();
	
		return $server_data;
	}
	
	function account_data($account){
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('*');
		$query->from('#__almh_whmaccounts');
		$query->where('WhmaccId = '.$account);
		$db->setQuery($query);
		$db->setQuery((string) $query);
		
		if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
		$account_data = $db->loadObject();
		
		return $account_data;	
	}
	
	function client_data($client){
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('a.*');
		$query->from('#__almh_clients AS a');
		$query->where('a.ClientId = '.$client);
		$query->select('e.email AS email');
		$query->join('LEFT', '#__users AS e ON e.id = a.ClientUser');
		$db->setQuery($query);
		$db->setQuery((string) $query);
		
		if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
		$client_data = $db->loadObject();
		
		return $client_data;	
	}
	
	function order_data($order){
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('a.*');
		$query->from('#__almh_orders AS a');
		$query->where('a.OrderId = '.$order);
		$db->setQuery($query);
		$db->setQuery((string) $query);
		
		if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
		$order_data = $db->loadObject();
		
		return $order_data;	
	}
	
	function invoice_data($client){
		
		$db		= JFactory::getDbo();
		$query	= $db->getQuery(true);
		$query->select('a.*');
		$query->from('#__almh_invoices AS a');
		$query->where('a.InvoiceClient = '.$client);
		$query->order('InvoiceId DESC LIMIT 1');
		$db->setQuery($query);
		$db->setQuery((string) $query);
		
		if (!$db->query()) {
				JError::raiseError(500, $db->getErrorMsg());
			}	
		$invoice_data = $db->loadObject();
		
		return $invoice_data;	
	}
	
	function curldata($key,$function){		
			
		$srv = server_data($key);	
		if ($srv->usessl == 1) {
				$htps = "https";
				$port = "2087";
			} elseif ($srv->usessl == 0) {
				$htps = "http";
				$port = "2086";
		}	
		$whmurl = "".$htps."://".$srv->ServerIp.":".$port."/xml-api/".$function;
		
		$curl = curl_init();
		$xml_url = $whmurl;
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		
		$header[0] = "Authorization: WHM $srv->ServerUsername:" . preg_replace("'(\r|\n)'", "",$srv->ServerPass);
		
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		curl_setopt($curl, CURLOPT_URL, $xml_url);
		
		$curldata = curl_exec($curl);
		
		curl_close($curl);
		
		return $curldata;
	}
	
	function plan_data($plan){
		
		$plan_data = "listpkgs";
		
		return $plan_data;
	}
	
	function createacc($acc){
		
		$acc_data = account_data($acc);
		$createacc =	"createacct?".
						"username=". $acc_data->WhmaccUser . "".
						"&password=". $acc_data->WhmaccPassword ."".
						"&domain=". $acc_data->WhmaccDomain ."".
						"&plan=". $acc_data->WhmaccPlan ."".
						"&contactemail=". $acc_data->WhmaccEmail ."";
		
		return $createacc;
	}
	
	function suspendacc($acc){
		
		$acc_data = account_data($acc);
		$url = 	"suspendacct?user=". strtolower($acc_data->WhmaccUser);
		
			if ($acc_data->WhmaccNote == null){
				$suspendacc = $url;
			}else{
				$suspendacc = $url . "&reason=" . str_replace(" ", "%20", $acc_data->WhmaccNote);
			}
		return $suspendacc;
	}
	
	function unsuspendacc($acc){
		
		$acc_data = account_data($acc);
		$unsuspendacc = "unsuspendacct?user=". $acc_data->WhmaccUser;
		
		return $unsuspendacc;
	}
	
	function listacc() {
			
		$listacc = "listaccts";
	
		return $listacc;
		
	}
	
	
	
?>